package com.xuanyuan.atlas.controler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xuanyuan.atlas.from.TableFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;

/**
 * @Description：
 * @Author： LUOYUAN
 * @Date： 2020-05-06-16:47
 */

@RestController
public class controller {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${atlas.address}")
    private  String atlasAddress;

    @Value("${atlas.port}")
    private  String atlasHost;

    public static final String BASE_URI = "/api/atlas/";
    private static final String DISCOVERY_URI      = BASE_URI + "v2/search";
    private static final String DSL_URI            = DISCOVERY_URI + "/dsl";
    private static final String FULL_TEXT_URI      = DISCOVERY_URI + "/fulltext";
    private static final String BASIC_SEARCH_URI   = DISCOVERY_URI + "/basic";
    private static final String FACETED_SEARCH_URI = BASIC_SEARCH_URI;



    /**
     * @Description:  获取所有表
     * @Author: luoy
     * @param jsonObject :
     * @Date: 2020/5/7 17:28
     */


  /*  {
        "excludeDeletedEntities":true,
            "includeSubClassifications":true,
            "includeSubTypes":true,
            "includeClassificationAttributes":true,
            "entityFilters":null,
            "tagFilters":null,
            "attributes":[

    ],
        "limit":500,
            "offset":0,
            "typeName":"hive_table",
            "classification":null,
            "termName":null
    }*/
    @PostMapping("/search/basic")
    public String getSearchBasic(@RequestBody JSONObject jsonObject){
        String url =getHost()+BASIC_SEARCH_URI;
        ResponseEntity<String> exchange = restTemplate.postForEntity(url,jsonObject, String.class);
        return exchange.getBody();

    }




    /**
     * @Description: 获取血缘关系
     * @Author: luoy
     * @param guid :
     * @Date: 2020/5/7 15:20
     */
    @GetMapping("/lineage/{guid}")
    public String getLineage(@PathVariable(value = "guid")String guid){
        String url =getHost()+BASE_URI+ "/v2/lineage/{guid}";
        ResponseEntity<String> exchange = restTemplate.getForEntity(url, String.class,guid);
        return exchange.getBody();

    }


    /**
     * @Description: 获取实体详细信息
     * @Author: luoy
     * @param guid :
     * @Date: 2020/5/7 17:09
     */
    @GetMapping("/entity/{guid}")
    public String getEntity(@PathVariable(value = "guid")String guid){
        String url =getHost()+BASE_URI+ "/v2/entity/guid/{guid}";
        ResponseEntity<String> exchange = restTemplate.getForEntity(url, String.class,guid);
        return exchange.getBody();

    }


    private String getHost(){
        return  "http://"+atlasAddress+":"+atlasHost;
    }

    private String getHost(int n){
        return  "http://"+atlasAddress+":"+atlasHost;
    }
    public Object getAllLineage(int workSpacedId) {
        TableFrom tableFrom = new TableFrom();
        tableFrom.setTypeName("hive_table");
        String url = getHost(workSpacedId) + BASIC_SEARCH_URI;
        ResponseEntity<JSONObject> exchange = restTemplate.postForEntity(url, tableFrom, JSONObject.class);
        JSONArray entities = exchange.getBody().getJSONArray("entities");
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i<entities.size();i++){
            JSONObject jsonObject = entities.getJSONObject(i);
            String guid = jsonObject.getString("guid");
            strings.add(guid);
        }
        ArrayList<Mono<Object>> lineages = new ArrayList<>();
        Mono<Object> resp;
        for (int i = 0; i < strings.size() ;i++){
            String basicAuth = "Basic "+ Base64.getEncoder().encodeToString("admin:admin".getBytes(StandardCharsets.UTF_8));
            resp = WebClient.create()
                    .get()
                    .uri("http://10.0.13.219:21000/api/atlas/v2/lineage/{guid}",strings.get(i))
                    .header(HttpHeaders.AUTHORIZATION,basicAuth)
                    .retrieve()
                    .bodyToMono(Object.class);
            lineages.add(resp);
        }
        ArrayList<Object> objects = new ArrayList<>();
        for (Mono<Object> lineage : lineages) {
            Object block = lineage.block();
            objects.add(block);
        }
        return objects;
    }
}
